package com.zmz.Stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2022/07/31/19:14
 * @Description: sorted, 中间操作。有两种排序：
 *
 * 1.sorted(): 自然排序，流中元素需实现Comparable接口
 *
 * 2.sorter(Comparator com) : Comparator 排序器自定义排序
 */
public class Stream12 {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("的撒", 8000, 22, "女", "武汉"));
        personList.add(new Person("法按时", 7000, 22, "女", "武汉"));
        personList.add(new Person("的阿萨", 7800, 22, "男", "武汉"));
        personList.add(new Person("答案", 12000, 12, "女", "武汉"));
        personList.add(new Person("滴放", 22000, 21, "女", "北京"));
        //按员工工资升序（自然排序），并取出所有名字
        List<String> names = personList.stream().sorted(Comparator.comparingInt(Person::getSalary)).map(p->p.getName()).collect(Collectors.toList());
        System.out.println(names);
        //按员工工资倒序
        List<String> names2 = personList.stream().sorted(Comparator.comparingInt(Person::getSalary).reversed()).map(p->p.getName()).collect(Collectors.toList());
        System.out.println(names2);
    }
}
